/****************************************************************
* @file   : test_main.c
* @author : NingJian (freegodly@gmail.com)
* @date   : 06/08/21 15:06:05
* @brief  : 
****************************************************************/

#include <stdio.h>
#include <stdlib.h>

#include <cmat.h>
#include <detection.h>

#define MAX_INPUT_DATA_LEN 307200

#define nA 3
#define nC 20

int main(int argc, char *argv[])
{
    printf("main run ... \n");
    printf("useage \n");
    printf("./test mode path \n");
    printf("./test 0 [big = 0,medium = 1,small = 2]  path [data/big_in.txt] \n");
    printf("./test  2  /home/ningjian/Code/torch_forward_c/data/small_in.txt \n");

    //##############################################################################################
    // 初始化可变参数
    int stride_big = 32;
    int stride_medium = 16;
    int stride_small = 8;

    int anchors_dim[2] = {3, 2};
    CMat *anchors_big = CMat_create(2, anchors_dim);
    anchors_big->data_ptr[0] = 3.625;
    anchors_big->data_ptr[1] = 2.8125;
    anchors_big->data_ptr[2] = 4.875;
    anchors_big->data_ptr[3] = 6.1875;
    anchors_big->data_ptr[4] = 11.65625;
    anchors_big->data_ptr[5] = 10.1875;

    CMat *anchors_medium = CMat_create(2, anchors_dim);
    anchors_medium->data_ptr[0] = 1.875;
    anchors_medium->data_ptr[1] = 3.8125;
    anchors_medium->data_ptr[2] = 3.875;
    anchors_medium->data_ptr[3] = 2.8125;
    anchors_medium->data_ptr[4] = 3.6875;
    anchors_medium->data_ptr[5] = 7.4375;

    CMat *anchors_small = CMat_create(2, anchors_dim);
    anchors_small->data_ptr[0] = 1.25;
    anchors_small->data_ptr[1] = 1.625;
    anchors_small->data_ptr[2] = 2.0;
    anchors_small->data_ptr[3] = 3.75;
    anchors_small->data_ptr[4] = 4.125;
    anchors_small->data_ptr[5] = 2.875;

    int p_info_small[4] = {1, 75, 64, 64};
    int p_info_medium[4] = {1, 75, 32, 32};
    int p_info_big[4] = {1, 75, 16, 16};

    //CMat_print(anchors);

    if (argc != 3)
        return 0;

    int mode = atoi(argv[1]);
    char *file_path = argv[2];

    // read input data
    double input_buff[MAX_INPUT_DATA_LEN];
    FILE *fp;
    char str[256];
    if ((fp = fopen(file_path, "rt")) == NULL)
    {
        puts("Fail to open file!");
        exit(0);
    }
    size_t data_count = 0;
    while (fgets(str, 256, fp) != NULL)
    {
        input_buff[data_count] = atof(str);
        ++data_count;
    }
    fclose(fp);
    printf("read data count = %d \r\n", data_count);

    //###########################################################

    // review data p

    CMat *p;
    if (mode == 0)
    {
        p = CMat_create(4, p_info_big);
    }
    else if (mode == 1)
    {
        p = CMat_create(4, p_info_medium);
    }
    else if (mode == 2)
    {
        p = CMat_create(4, p_info_small);
    }

    {
        size_t i = 0;
        for (i = 0; i < data_count; ++i)
        {
            p->data_ptr[i] = input_buff[i];
        }
    }
    //CMat_print(p);

    CMat *result;

    if (mode == 0)
    {
        result = detection_forwad(p, anchors_big, nA, nC, stride_big, 1);
    }
    else if (mode == 1)
    {
        result = detection_forwad(p, anchors_medium, nA, nC, stride_medium, 1);
    }
    else if (mode == 2)
    {
        result = detection_forwad(p, anchors_small, nA, nC, stride_small, 1);
    }

    CMat_print(result);

    //#######################################################################
    // out put to file

    if ((fp = fopen("result_100.txt", "wat")) == NULL)
    {
        puts("Fail to open file!");
        exit(0);
    }

    int i = 0;
    for(i = 0; i< result->data_len; ++i)
    {
        fprintf(fp, "%f\r\n", result->data_ptr[i]);
    }
    fclose(fp);


    return 0;
}
